//
//  StrategyForeignStrategyViewController.m
//  ITrip
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyForeignStrategyViewController.h"
#import "StrategyButtonViewController.h"

@interface StrategyForeignStrategyViewController ()<UIAlertViewDelegate>

@end

@implementation StrategyForeignStrategyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImage *icon = [UIImage imageNamed:@"iconfont_return"];
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleDone target:self action:@selector(getBack)];
    
    self.view.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    NSString *str = @"攻略";
    title.text = [self.titleName stringByAppendingString:str];
    
    title.font = [UIFont systemFontOfSize:20];
    title.font = [UIFont boldSystemFontOfSize:25];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    
    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];
    
    NSString *path = [URLPath strategyForeignStrategyURL];
    self.arrayData = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [path stringByAppendingFormat:@"%ld.json?page=1", self.numberStrategy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.array = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dic in self.array) {
            self.strategyParser = [[StrategyForeignStrategyParser alloc]init:dic];
            [self.arrayData addObject:self.strategyParser];
            [activityIndicator stopAnimating];
        }
        [self createMyView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        alert.delegate = self;
        [alert show];
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];
    
    NSString *path = [URLPath strategyForeignStrategyURL];
    self.arrayData = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [path stringByAppendingFormat:@"%ld.json?page=1", self.numberStrategy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.array = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dic in self.array) {
            self.strategyParser = [[StrategyForeignStrategyParser alloc]init:dic];
            [self.arrayData addObject:self.strategyParser];
            [activityIndicator stopAnimating];
        }
        [self createMyView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        alert.delegate = self;
        [alert show];
    }];
}

- (void)getBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createMyView
{
    //创建布局类
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
#pragma -mark设置方块的大小，要把高度改成自适应的
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 10, 40);
//    layout.minimumInteritemSpacing = 10;
//    layout.minimumLineSpacing = 10;
    myForeignStrategy = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    myForeignStrategy.delegate = self;
    myForeignStrategy.dataSource = self;
    [myForeignStrategy registerClass:[StrategyForeignStrategyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    myForeignStrategy.showsVerticalScrollIndicator = NO;
    
    //Header View 注册
    [myForeignStrategy registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    [self.view addSubview:myForeignStrategy];
    myForeignStrategy.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
//    [myForeignStrategy release];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyForeignStrategyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    StrategyForeignStrategyParser *forParser = [self.arrayData objectAtIndex:indexPath.section];
    StrategyPagesParser *pages = [forParser.pages objectAtIndex:indexPath.row];
    [cell setPagesParser:pages];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    StrategyForeignStrategyParser *forParser = [self.arrayData objectAtIndex:section];
    return forParser.pages.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.arrayData.count;
}


//分区section的文字
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    StrategyForeignStrategyParser *strategyParser = [self.arrayData objectAtIndex:indexPath.section];
    UICollectionReusableView *reusableView;
    if (kind == UICollectionElementKindSectionHeader) {
        HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor whiteColor];

//        NSLog(@"%ld", strategyParser.stratrgy_category_type);

            if (strategyParser.stratrgy_category_type == 0) {
                headerView.label.text = @"概览";
            } else if (strategyParser.stratrgy_category_type == 1) {
            headerView.label.text = @"出行须知";
            }  else if (strategyParser.stratrgy_category_type == 2) {
             headerView.label.text = @"如何到达";
            } else if (strategyParser.stratrgy_category_type == 3) {
            headerView.label.text = @"当地交通";
            } else if (strategyParser.stratrgy_category_type == 4) {
            headerView.label.text = @"景点";
            } else if (strategyParser.stratrgy_category_type == 5) {
            headerView.label.text = @"娱乐";
            } else if (strategyParser.stratrgy_category_type == 6) {
            headerView.label.text = @"住宿";
            } else if (strategyParser.stratrgy_category_type == 7) {
            headerView.label.text = @"美食";
            } else if (strategyParser.stratrgy_category_type == 8) {
            headerView.label.text = @"购物";
            } else if (strategyParser.stratrgy_category_type == 9) {
            headerView.label.text = @"离境须知";
            } else if (strategyParser.stratrgy_category_type == 11) {
            headerView.label.text = @"其它";
            }
        
        reusableView = headerView;
    }
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyButtonViewController *strategyButton = [[StrategyButtonViewController alloc] init];
    strategyButton.arrayData = self.arrayData;
    strategyButton.row = indexPath.row;
    strategyButton.section = indexPath.section;
    [self.navigationController pushViewController:strategyButton animated:YES];
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 40);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
